﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JCNT.DAL;
using Pub;

namespace JCNT.NoteApp
{
    public partial class FLab_Add : Form
    {
        CTreeII ctree = new CTreeII();
        public string ID;
        public string RootID;
        public int EDT_STATE;       //0:null; 1:Add; 2:Edit
        public TreeNodeII nodeSelected;

        public FLab_Add()
        {
            InitializeComponent();
        }

        private void FLab_Add_Load(object sender, EventArgs e)
        {
            string strSQL = "SELECT Nam FROM T_Highlight";

            cboxHighlight.Items.Clear();
            CDB.fillComboBox(strSQL, ref cboxHighlight);
        }

        public void showVal(string ID)
        {
            this.ID = ID;
            Cwp_nlab wp_nlab = ctree.get_wp_nlab(this.ID);
            if (wp_nlab == null) return;

            this.RootID = wp_nlab.RootID;
            refTree();

            if (EDT_STATE == 2)
            {
                tboxLab.Text = wp_nlab.Lab;
                cboxVis.Checked = wp_nlab.Vis == "1" ? true : false;
            }
            else
            {
                tboxLab.Text = "";
                cboxVis.Checked = true;
            }
        }

        public void showVal(TreeNodeII node1)
        {
            this.nodeSelected = node1;
            this.ID = node1.ID;
            Cwp_nlab wp_nlab = ctree.get_wp_nlab(this.ID);
            if (wp_nlab == null) return;

            this.RootID = wp_nlab.RootID;
            refTree();

            if (EDT_STATE == 2)
            {
                tboxLab.Text = wp_nlab.Lab;
                cboxHighlight.Text = node1.sHighlight;
                cboxVis.Checked = wp_nlab.Vis == "1" ? true : false;
            }
            else
            {
                tboxLab.Text = "";
                cboxVis.Checked = true;
            }
        }

        public void refTree()
        {

            TreeNode node = new TreeNode();
            node.Tag = "0";
            node.Text = @"\";
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;


        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            int i_btn = 0;
            if (sender is ToolStripMenuItem)
                i_btn = Convert.ToInt16(((ToolStripMenuItem)sender).Tag);

            if (sender is ToolStripButton)
                i_btn = Convert.ToInt16(((ToolStripButton)sender).Tag);

            if (sender is Button)
                i_btn = Convert.ToInt16(((Button)sender).Tag);

            if (sender is NotifyIcon)
                i_btn = Convert.ToInt16(((NotifyIcon)sender).Tag);

            switch (i_btn)
            {
                //OK
                case 1:
                    //ok
                    TreeNodeII node2 = new TreeNodeII();
                    node2.sLab = tboxLab.Text;
                    node2.sHighlight = cboxHighlight.Text;
                    node2.sVis = cboxVis.Checked ? "1" : "0";
                    node2.Text = tboxLab.Text;

                    //Add
                    if (EDT_STATE == 1)
                    {
                        string sID = ctree.newLabID();
                        node2.ID = sID;
                        node2.RootID = nodeSelected.ID;
                        ctree.labAdd(node2);
                        //目录直接添加到窗口列表中
                        //Program.ftree.tViewLab.SelectedNode.Nodes.Add(node2);
                        Program.ftree.ref_Tree();
                    }
                    //Edit
                    else if (EDT_STATE == 2)
                    {
                        node2.ID = nodeSelected.ID;
                        node2.RootID = nodeSelected.RootID;
                        ctree.labEdt(node2);
                        //直接修改窗口列表中的名称
                        Program.ftree.tViewLab.SelectedNode.Text = node2.Text;
                    }
                    Close();
                    Program.fmain.Focus();
                    break;
                //cancel
                case 2:
                    //cancel
                    Close();
                    Program.fmain.Focus();
                    break;
                case 11:     //Add
                    Program.flabAdd.EDT_STATE = 1;
                    Program.flabAdd.showVal(this.ID);
                    Program.flabAdd.ShowDialog();
                    break;
                case 12:     //edit
                    Program.flabAdd.EDT_STATE = 2;
                    Program.flabAdd.showVal(this.ID);
                    Program.flabAdd.ShowDialog();
                    break;
                case 13:     //delete
                    if (CPub.MsgBox("确定要删除吗？", "删除"))
                    {
                        ctree.labDel(nodeSelected);

                    }
                    break;
                case 14:
                    refTree();

                    break;
            }
        }

        private void FLab_Add_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

    }
}
